// pages/admin/income/lookDepartment/lookDepartment.js
const app = getApp();
import Api from '../../../../utils/http'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchFlag: true,

    addTimeStatus: 1, //筛选-添加时间, 1 升序，2降序
    putTimeStatus: 1, //筛选-投放时间, 1 升序，2降序
    // nameStatus: 1,//筛选-名称排序, 1 降序，2升序
    page: 2, //页码
    pageSize: 6, //每页显示条数
    departList: [], //部门列表
    put: '',
    code: null,
  },
  //跳转 部门详情
  toDepartDetail: function (e) {
    var that = this;
    var {
      departList
    } = that.data
    // console.log(e.currentTarget.dataset)
    let index = e.currentTarget.dataset.index
    var departName = departList[index].title
    var lid = departList[index].id
    wx.navigateTo({
      url: `/pages/admin/department/departDetail/departDetail?departName=${departName}&lid=${lid}`,
    })
  },
  //点击输入框
  searchClick: function () {
    this.setData({
      searchFlag: false
    })
  },
  //搜索
  search: function (e) {
    var that = this;
    that.setData({
      put: e.detail.value
    });
    that.getseachDepartmentList()
  },
  //筛选-添加时间, 1 升序，2降序
  changeAddTimeStatus: function () {
    var that = this;
    var addTimeStatus = that.data.addTimeStatus;
    if (addTimeStatus == 1) {
      addTimeStatus = 2;
    } else {
      addTimeStatus = 1;
    }
    that.setData({
      addTimeStatus: addTimeStatus
    })
    that.getaddDepartmentList();
    console.log(that.data.addTimeStatus)
  },
  //筛选-投放时间, 1升序，2 降序
  changePutTimeStatus: function () {
    var that = this;
    var putTimeStatus = that.data.putTimeStatus;
    if (putTimeStatus == 1) {
      putTimeStatus = 2;
    } else {
      putTimeStatus = 1;
    }
    that.setData({
      putTimeStatus: putTimeStatus
    })
    that.getputDepartmentList();
    console.log(that.data.putTimeStatus)
  },
  // //筛选-名称排序, 1 降序，2升序
  // changeNameStatus: function(){
  //   var that = this;
  //   var nameStatus = that.data.nameStatus;
  //   if(nameStatus == 1){
  //     nameStatus = 2;
  //   }else{
  //     nameStatus = 1;
  //   }
  //   that.setData({
  //     nameStatus: nameStatus
  //   })
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.getDepartmentList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.refreshData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.requestMoreData();
  },
  /**下拉刷新*/
  refreshData() {
    var that = this;
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      pageNum: 1, // 页码默认为0
      pageSize: 9 // 每页显示合数，默认为10
    }
    Api.ajax('/app/Zmanage/departmentList', param).then(res => {
      console.log(res)
      if (res.code == 200) {

        that.setData({
          departList: res.data.list,
          page: 2 // 一定要记得重置页码 ---- 没有数据的提示（上拉加载提示过后）
        })
        // 真机测试的时候，下拉刷新技术需要停止 下拉刷新的操作
        wx.stopPullDownRefresh();
      }
    }).catch((err) => {
      console.log(err)
    })
  },
  /**请求更多信息*/
  requestMoreData() {
    var that = this;
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      pageNum: that.data.page,
      pageSize: 9
    }
    console.log(param)
    Api.ajax('/app/Zmanage/departmentList', param).then(res => {
      // 请求之后 需要判断 
      // 1.判断有没有数据
      console.log(res)
      if (res.code == 200) {
        // 2.如果有数据 --- 之前的数据追加上现在请求的数据  数组合并
        // 3.每一次请求完成页面要完成自动加1
        let arr = that.data.departList // 获取数据
        let num = that.data.page
        let lists = [...arr, ...res.data.list] // 处理数据
        num += 1
        that.setData({ // 修改数据
          departList: lists,
          page: num
        })
      } else {
        // 没有更多数据了`
        wx.showToast({
          title: "没有更多数据了",
          icon: "none",
          duration: 2000,
        })
      }
    })
  },
  /**个人信息*/
  getuserInfo: function () {
    var that = this;
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      user_type: uid.user_type
    }
    console.log(param)
    Api.ajax('/app/Zpersonalcenter/userInfo', param).then(res => {
      console.log(res)
      if (res.code == 200) {
        if (res.data.user_type == 3) {
          that.setData({
            identityStatus: 0
          })
        } else if (res.data.user_type == 2) {
          that.setData({
            identityStatus: 1
          })
        }
      }
    })
  },
  //投放时间排序
  getputDepartmentList: function () {
    var that = this;
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      pageNum: 1,
      pageSize: 9,
      order_puttime: that.data.putTimeStatus,
    };
    console.log(param);
    Api.ajax('/app/Zmanage/departmentList', param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          data: res.data,
          departList: res.data.list
        })
      } else {
        wx.showToast({
          title: "暂时没有",
          icon: "none",
          duration: 2000,
        })
      }
    })
  },
  //添加时间排序
  getaddDepartmentList: function () {
    var that = this;
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      pageNum: 1,
      pageSize: 9,
      order_addtime: that.data.addTimeStatus,
    };
    console.log(param);
    Api.ajax('/app/Zmanage/departmentList', param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          data: res.data,
          departList: res.data.list
        })
      } else {
        wx.showToast({
          title: "暂时没有",
          icon: "none",
          duration: 2000,
        })
      }
    })
  },
  //搜索
  getseachDepartmentList: function () {
    var that = this;
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      pageSize: that.data.pageSize,
      title: that.data.put
    };
    console.log(param);
    Api.ajax('/app/Zmanage/departmentList', param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          data: res.data,
          departList: res.data.list
        })
      } else {
        wx.showToast({
          title: "暂时没有",
          icon: "none",
          duration: 2000,
        })
      }
    })
  },
  /**获取部门列表信息*/
  getDepartmentList: function () {
    var that = this;
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      pageNum: 1,
      pageSize: that.data.pageSize,
      // order_addtime: that.data.addTimeStatus,
      // order_puttime: that.data.putTimeStatus,
      // title: that.data.launch.toString()
    };
    console.log(param);
    Api.ajax('/app/Zmanage/departmentList', param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          code:res.code,
          data: res.data,
          departList: res.data.list
        })
      } else {
        wx.showToast({
          title: "暂时没有",
          icon: "none",
          duration: 2000,
        })
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})